package com.xmkj.carwash.count;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.common.base.Config;
import com.common.carbean.CountInfoBean;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.entity.bean.UserBean;
import com.common.retrofit.methods.LoginMethods;
import com.common.retrofit.methods.ReallyThreeLoginMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.RegexUtils;
import com.common.utils.SPUtils;
import com.xmkj.carwash.MainActivity;
import com.xmkj.carwash.R;
import com.xmkj.carwash.activity.LoginBindPhoneActivity;
import com.xmkj.payandlogin.LoginUtil;
import com.xmkj.payandlogin.ShareConfig;
import com.xmkj.payandlogin.ShareManager;
import com.xmkj.payandlogin.login.LoginListener;
import com.xmkj.payandlogin.login.LoginPlatform;
import com.xmkj.payandlogin.login.LoginResult;
import com.xmkj.payandlogin.login.result.QQUser;
import com.xmkj.payandlogin.login.result.WxUser;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by chen on 2018/3/22.
 */

public class LoginActivtiy extends BaseMvpActivity {
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_psw)
    EditText mEtPsw;
    @BindView(R.id.tv_register)
    TextView mTvRegister;
    @BindView(R.id.tv_forget_psw)
    TextView mTvForgetPsw;
    @BindView(R.id.tv_intrue)
    TextView mTvIntrue;
    @BindView(R.id.flag)
    TextView mFlag;
    @BindView(R.id.iv_qq_login)
    ImageView mIvQqLogin;
    @BindView(R.id.iv_weichat_login)
    ImageView mIvWeichatLogin;
    private  String jpushid = "";
    private int type;
    private UserBean userBean;
    private String authId;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        ShareConfig config = ShareConfig.instance()
                .qqId(Config.APP_QQ_ID)
                .wxSecret(Config.APP_SERECET)
                .wxId(Config.APP_ID);
        ShareManager.init(config);
        attachClickListener(mTvForgetPsw);
        attachClickListener(mTvRegister);
        attachClickListener(mTvIntrue);
        attachClickListener(mIvQqLogin);
        attachClickListener(mIvWeichatLogin);
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==mTvForgetPsw.getId()){
            gotoActivity(FindMyPswActivity.class);
        } else if(view.getId()==mTvRegister.getId()){
            gotoActivity(RegisterActivity.class);
        }else if(view.getId()==mTvIntrue.getId()){
            if (EmptyUtils.isEmpty(getEditTextStr(mEtPhone))) {
                showToastMsg("请输入手机号");
                return;
            } else if (!RegexUtils.isMobileExact(getEditTextStr(mEtPhone))) {
                showToastMsg("手机号不正确");
                return;
            } else  if (EmptyUtils.isEmpty(getEditTextStr(mEtPsw))) {
                showToastMsg("请输入密码");
                return;
            }
            gotoLogin();
        } else if(mIvWeichatLogin.getId()==view.getId()){
            type = 2;
            LoginUtil.login(this, LoginPlatform.WX, mLoginListener);
        } else if(mIvQqLogin.getId()==view.getId()){
            type = 1;
            LoginUtil.login(this, LoginPlatform.QQ, mLoginListener);
        }
    }

    private void gotoLogin() {
        showProgressingDialog();
        CommonSubscriber<CountInfoBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                CountInfoBean infoBean = (CountInfoBean) o;
                SPUtils.setShareInt(Constans.UID,infoBean.uid);
                SPUtils.setShareString(Constans.HASHID,infoBean.hashid);
                dismissProgressDialog();
                gotoActivity(MainActivity.class);
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        jpushid = JPushInterface.getRegistrationID(this);
        LoginMethods.getInstance().login(subscriber,getEditTextStr(mEtPhone),getEditTextStr(mEtPsw),jpushid);
        rxManager.add(subscriber);
    }

    private LoginListener mLoginListener = new LoginListener() {
        @Override
        public void loginSuccess(LoginResult result) {
           /* Toast.makeText(LoginActivity.this,
                    result.getUserInfo() != null ? result.getUserInfo().getNickname()
                            : "" + "登录成功", Toast.LENGTH_SHORT).show();*/
            // 处理result
            switch (result.getPlatform()) {
                case LoginPlatform.QQ:
                    QQUser user = (QQUser) result.getUserInfo();
                    String qqauthId = user.getOpenId();
                    authId = user.getOpenId();
                    String qqauthName = user.getNickname();
                    String qqauthHead = user.getHeadImageUrl();
                    String qqauthGender = user.getSex()+"";
                    //gotoReq(2,qqauthId,qqauthName,qqauthHead,qqauthGender);
                    otherLoginSuccesss(qqauthId,qqauthName,qqauthHead,qqauthGender);
                    break;
                case LoginPlatform.WX:
                    WxUser wxUser = (WxUser) result.getUserInfo();
                    String wxauthId = wxUser.getOpenId();
                    authId = wxUser.getOpenId();
                    String wxauthName = wxUser.getNickname();
                    String wxauthHead = wxUser.getHeadImageUrl();
                    String wxauthGender = wxUser.getSex()+"";
                    otherLoginSuccesss(wxauthId,wxauthName,wxauthHead,wxauthGender);

                    break;
            }
        }

        @Override
        public void loginFailure(Exception e) {
            Toast.makeText(LoginActivtiy.this, "登录失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void loginCancel() {
            Toast.makeText(LoginActivtiy.this, "登录取消", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void setStatusBar() {

    }
    private void otherLoginSuccesss(final String qqauthId, String qqauthName, String qqauthHead, String qqauthGender) {
        CommonSubscriber<UserBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                userBean = (UserBean) o;
                if(EmptyUtils.isEmpty(userBean.mobile)){ //未绑定手机
                    Intent intent = new Intent(context,LoginBindPhoneActivity.class);
                    intent.putExtra(LoginBindPhoneActivity.USERBEAN,userBean);
                    intent.putExtra(LoginBindPhoneActivity.AUTHID,authId);
                    startActivity(intent);
                }else {
                    SPUtils.setShareInt(Constans.UID,userBean.uid);
                    SPUtils.setShareString(Constans.HASHID,userBean.hashid);
                    dismissProgressDialog();
                    gotoActivity(MainActivity.class);
                }

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        jpushid = JPushInterface.getRegistrationID(this);

        ReallyThreeLoginMethods.getInstance().oauthLogin(subscriber,type,qqauthId,qqauthName,qqauthHead,jpushid);
        rxManager.add(subscriber);
    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
